% LaTeX tgrind environment
%
% $Header: tgrind.sty,v 1.3 88/08/14 17:43:54 van Exp $
%
% 

\newif\ifcomment\newif\ifstring
\newbox\linesofar
\newdimen\TBwid
\newdimen\ts
\newbox\tbox

% 
\def\tgrind{\@ifnextchar[{\@tgrind}{\@tgrind[t]}}
\def\@tgrind[#1]{%
\ifhmode\par\fi
\if #1n\newcount\linecount\newcount\linenext
\def\L##1{\vbox{\hbox to\hsize{\CF\strut\global\advance\linecount by1
##1\hss\linebox}}}
\linecount=0\linenext=9
\def\linebox{\ifnum\linecount>\linenext\global\advance\linenext by10
\hbox{\tiny\the\linecount}\fi}\else
\def\L##1{\vbox{\hbox{\CF\strut ##1}}}\fi
% Stuff we ignore.
\let\Head=\@gobble
\def\File##1,##2,##3{}
\let\Proc=\@gobble
\let\ProcCont=\@gobble
%
% Each formfeed in the input is replaced by a "\NewPage" macro.  If
% you really want a page break here, define this as "\vfill\eject".
\def\NewPage{\filbreak\bigskip}
%
\def\LB{\CF\hbox}
\setbox\linesofar=\null
\everyhbox={\box\linesofar}
\def\Tab##1{\setbox\tbox=\lastbox\TBwid=1\wd\tbox\advance\TBwid by 1\ts
\ifdim\TBwid>##1\ts
\setbox\linesofar=\hbox{\box\tbox\Space}\else
\setbox\linesofar=\hbox to ##1\ts{\box\tbox\hfil}\fi\LB}
%
% A normal space is too thin for code listings.  We make spaces & tabs
% be in "\ts" units (which are the width of a "0" in the current font).
\setbox\tbox=\hbox{0} \ts=1\wd\tbox \setbox\tbox=\hbox{\hskip 1\ts}
\def\Space{\hskip 1\ts\relax}
%
% Font changing stuff for keywords, comments & strings.  We put keywords
% in boldface, comments in text-italic & strings in typewriter.  Since
% we're usually changing the font inside of a \LB macro, we remember the
% current font in \CF & stick a \CF at the start of each new box.
% Also, the characters " and ' behave differently in comments than in
% code, and others behave differently in strings than in code.
\let\CF=\rm
\def\K##1{{\bf ##1}}	% Keyword
\def\C{\it\global\let\CF=\it\global\commenttrue\relax}	% Comment Start
\def\CE{\rm\global\let\CF=\rm\global\commentfalse\relax}% Comment End
\def\S{\tt\global\let\CF=\tt\global\stringtrue\relax}	% String Start
\def\SE{\rm\global\let\CF=\rm\global\stringfalse\relax}	% String End
%
% Special characters.
\def\{{\ifmmode\lbrace\else\ifstring{\char'173}\else$\lbrace$\fi\fi}
\def\}{\ifmmode\rbrace\else\ifstring{\char'175}\else$\rbrace$\fi\fi}
\def\!{\ifmmode\backslash\else\ifstring{\char'134}\else$\backslash$\fi\fi}
\def\|{\ifmmode|\else\ifstring{\char'174}\else$|$\fi\fi}
\def\<{\ifmmode<\else\ifstring<\else$<$\fi\fi}
\def\>{\ifmmode>\else\ifstring>\else$>$\fi\fi}
\def\/{\ifmmode/\else\ifstring/\else$/$\fi\fi}
\def\*{\ifmmode*\else\ifstring*\else$*$\fi\fi}
\def\-{\ifmmode-\else\ifstring-\else$-$\fi\fi}
\def\_{\ifstring{\char'137}\else\underline{\Space}\fi}
\def\&{{\char'046}}
\def\#{{\char'043}}
\def\%{{\char'045}}
\def\~{{\char'176}}
\def\"{\ifcomment''\else{\tt\char'042}\fi}
\def\'{\ifcomment'\else{\tt\char'047}\fi}
\def\^{{\char'136}}
\def\${{\rm\char'044}}
\mathsurround 0pt
\@tobeyspaces\if #1c\let\etgrind@=\etgrind@c\[\vbox\bgroup
\else \if #1b\let\etgrind@=\etgrind@b \vbox\bgroup
\else\let\etgrind@=\relax\fi\fi
}
\let\etgrind@=\relax
\def\etgrind@c{\egroup\]}
\def\etgrind@b{\egroup}
\def\endtgrind{\etgrind@}
{\catcode`\ =\active\gdef\@tobeyspaces{\catcode`\ \active\let \Space}}
